package it.demosms.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonResponse;
import it.demosms.util.SmsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 监听类
 *
 * @author 季鑫利
 */
@Component
public class SmsMessageConsumer implements MessageListener {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SmsUtil smsUtil;

    @Value("${smsCode}")
    private String smsCode;

    @Value("${param}")
    private String param;


    @Override
    @RabbitListener(queues = "topic_queue")
    public void onMessage(Message message) {
        String jsonString = new String(message.getBody());
        Map map = JSON.parseObject(jsonString);
        String phone = (String) map.get("phone");//手机号
        String code = (String) map.get("code");
        System.out.println("code = " + code);
        log.info(phone + ":" + code);
        //调用阿里云通信
        //用code替换配置文件中的[value]
        CommonResponse commonResponse = smsUtil.sendSms(phone, smsCode, param.replace("[value]", code));
    }
}
